package algorithm_demo.demo_advanced;

import javax.security.auth.login.CredentialException;

/**
 * 奶牛生小牛问题
 * 第一年农场有1只成熟的母牛A，往后的每年：
 * 1）每一只成熟的母牛都会生一只母牛
 * 2）每一只新出生的母牛都在出生的第三年成熟
 * 3）每一只母牛永远不会死
 * 返回N年后牛的数量
 * <p>
 * 利用线性代数，也可以改写出另一种表示 |F(N), F(N-1)|=|F(2),F(1)| * 某个二阶矩阵的N-2次方
 *
 * @author Api
 * @date 2023/3/26 12:43
 */
public class Code06 {
    public static int c1(int n) {
        if (n < 1) {
            return 0;
        }
        if (n == 1 || n == 2 || n == 3) {
            return n;
        }
        return c1(n - 1) + c1(n - 3);
    }

    public static int c2(int n) {
        if (n < 1) {
            return 0;
        }
        if (n == 1 || n == 2 || n == 3) {
            return n;
        }
        int res = 3;
        int pre = 2;
        int prepre = 1;
        int tmp1 = 0;
        int tmp2 = 0;
        for (int i = 4; i <= n; i++) {
            tmp1 = res;
            tmp2 = pre;
            res = res + prepre;
            pre = tmp1;
            prepre = tmp2;
        }
        return res;
    }
    /*矩阵*/
    /*
    * |F4F3F2| = |F3F2F1|*|3*3|1次方的矩阵
    * |F5F4F3| = |F4F3F2|*|3*3|1次方的矩阵
    *
    * 如果母牛10年后死了
    * F(N) = F(N-1)+F(N-3)-F(N-10)的10阶矩阵
    *
    * */
    public static int c3(int n) {
        if (n < 1) {
            return 0;
        }
        if (n == 1 || n == 2 || n == 3) {
            return n;
        }
        //三阶矩阵(通过正常公式推导出来，矩阵的各个值)
        int[][] base = {{1, 1, 0}, {0, 0, 1}, {1, 0, 0}};
        int[][] res = matrixPower(base, n - 3);
        return 3 * res[0][0] + 2 * res[1][0] + res[2][0];
    }

    public static int[][] matrixPower(int[][] m, int p) {
        int[][] res = new int[m.length][m[0].length];
        for (int i = 0; i < res.length; i++) {
            res[i][i] = 1;
        }
        //res = 矩阵中的1
        int[][] t = m;
        for (; p != 0; p >>= 1) {
            if ((p & 1) != 0) {
                res = muliMatrix(res, t);
            }
            t = muliMatrix(t, t);
        }
        return res;
    }

    public static int[][] muliMatrix(int[][] m1, int[][] m2) {
        int[][] res = new int[m1.length][m2[0].length];
        for (int i = 0; i < m1.length; i++) {
            for (int j = 0; j < m2[0].length; j++) {
                for (int k = 0; k < m2.length; k++) {
                    res[i][j] += m1[i][k] * m2[k][j];
                }
            }
        }
        return res;
    }

    /*
     * 一个人上台阶，一次可以上1一个台阶或者2个台阶，上n个台阶有多少种方法
     * */
    /*第一种方法：暴力递归*/
    public static int s1(int n) {
        if (n < 1) {
            return 0;
        }
        if (n == 1 || n == 2) {
            return n;
        }
        return s1(n - 1) + s1(n - 2);
    }

    /*第二种方法：*/
    public static int s2(int n) {
        if (n < 1) {
            return 0;
        }
        if (n == 1 || n == 2) {
            return n;
        }
        int res = 2;
        int pre = 1;
        int tmp = 0;
        for (int i = 3; i <= n; i++) {
            tmp = res;
            res = res + pre;
            pre = tmp;
        }
        return res;
    }

    /*第三种方法：矩阵乘法的方法*/
    public static int s3(int n) {
        if (n < 1) {
            return 0;
        }
        //|F(n),F(n-1) = |2,1|*|{1,1}{1,0}|
        if (n == 1 || n == 2) {
            return n;
        }
        int[][] base = {{1, 1}, {1, 0}};
        int[][] res = matrixPower(base, n - 2);
        return 2 * res[0][0] + res[1][0];
    }

    /*比如一个人可以迈1,2,5步 F(N) = F(N-1)+F(N-2)+F(N-5)*/
    /*F(N)F(N-1)F(N-2)F(N-2)F(N-3)F(N-4) = |5,4,3,2,1|*|{}，{}，{}，{}，{}|矩阵*/

    public static void main(String[] args) {
        System.out.println(s1(3));
        System.out.println(s2(3));
        System.out.println(s3(3));
    }


}
